import {Fetch} from '../libs/';
import {
    FETCH_USER_REQUEST,
    FETCH_USER_RECEIVE,
    TOGGLE_MENU,

    SELECT_NEWS_CATEGORY,
    SELECT_NEWS_LIST,

    SELECT_ACTIVITY_CATEGORY,
    SELECT_ACTIVITY_LIST,

    SELECT_NOTICE_CATEGORY,
    SELECT_NOTICE_LIST,

    FETCH_ACTION_REQUEST,
    FETCH_ACTION_RECEIVE,

    FETCH_ACTION_REMOVE_REQUEST,
    FETCH_ACTION_REMOVED_RECEIVE,


    FETCH_USER_CLASS_SELECT,
    FETCH_USER_CLASS_SELECT_ALL,
    FETCH_USER_CLASS_UNSELECT,

    SET_FORM_ITEM,

    ENABLE_DELETE_BTN,
    SELECT_ALL_ITEM,
    SELECT_ITEM,
    OPEN_MODAL,
    CLOSE_MODAL,
    UPDATE_CURRENT_NAV_ITEM,
    CLIENT_INFO_SET
} from './actionType';
import md5 from "blueimp-md5";

export function update_current_nav_item(subject, current) {
    return {
        type: UPDATE_CURRENT_NAV_ITEM,
        subject,
        current,
    }
}
function request_user(keyword) {
    return {
        type: FETCH_USER_REQUEST,
        keyword,
    }
}
/***
 *
 * @param index
 * @returns {{type, index: *}}
 */
export function select_user_class(index) {
    return {
        type: FETCH_USER_CLASS_SELECT,
        index
    }
}
function receive_user(user) {
    return {
        type: FETCH_USER_RECEIVE,
        user,
        receivedAt: Date.now()
    }
}

export function selectRequest(subject) {
    return {
        type: SELECT_NOTICE_LIST,
        subject,
    }
}

export function setFormItem(subject, formItem) {
    return {
        type: SET_FORM_ITEM,
        subject,
        item: formItem
    }
}
/**
 * should fetch logined user
 */
function shouldFetch(state, keyword) {
    const user = state.user_login.user;
    if (_.isEmpty(user)) {
        return true
    } else {
        return false
    }
}

function fetchUser_() {
    return dispatch => {
        dispatch(request_user())
        return Promise.all([F.fetch(URL.Get_USER), F.fetch(URL.Get_USER_CLASS)])
            .then(json => {
                const user = json[0].user || {};
                user.class = json[1].list;
                return dispatch(receive_user(user));
            })
            .catch(e=> {
                console.log(e)
            })
    }
}
function shouldFetchUser(state, subject) {
    const subject_ = state.user_login.user;
    if (!subject_.class.length && !state.user_login.isFetching) {
        return true
    } else {
        return false
    }
}
/***
 *  fetch login user
 */
export function fetchUser() {
    return (dispatch, getState) => {
        if (shouldFetchUser(getState())) {
            return dispatch(fetchUser_())
        }
    }
}

////common logic begin


/**
 * request fetch
 * @returns {{type}}
 */
function request_fetch(subject) {
    return {
        type: FETCH_ACTION_REQUEST,
        subject
    }
}
/**
 * receive fetch
 * @returns {{type}}
 */
function receive_fetch(subject, json, query) {
    return {
        type: FETCH_ACTION_RECEIVE,
        subject,
        json,
        query,
        receivedAt: Date.now()
    }
}
/**
 * should fetch subject
 */
function shouldFetch(state, subject, query) {
    const subject_ = state.fetchBySubject[subject];
    if (_.isEmpty(subject_) && !subject_.isFetching) {
        return true
    } else {
        return false
    }
}
/**
 * fetch action
 * @param subject
 * @returns {function(*)}
 */
function fetch_action(subject, query, state) {
    return dispatch => {
        dispatch(request_fetch(subject));
        query = Object.assign({}, state.fetchBySubject[subject].query, query);
        const url = query.url;
        delete query['url'];
        return Fetch(url, Object.assign({body: query}, query.POST ? {method: 'POST'} : {}))
            .then(json => {
                return dispatch(receive_fetch(subject, json, query));
            })
            .catch(e => {
                console.log(e);
            })
    }
}
/***
 *  fetch subject async
 */
export function fetch(query = {}) {
    return (dispatch, getState) => {
        const subject = md5(query.url.slice(0, query.url.indexOf('.')));

        if (shouldFetch(getState(), subject, query)) {
            return dispatch(fetch_action(subject, query, getState()))
        }
    }
}


function request_fetch_remove_request(subject) {
    return {
        type: FETCH_ACTION_REMOVE_REQUEST,
        subject,
    }
}

function request_fetch_removed_receive(subject, json, query) {
    return {
        type: FETCH_ACTION_REMOVED_RECEIVE,
        subject,
        json,
        ...query,
    }
}

function fetch_remove_action(subject, query) {
    return dispatch => {
        dispatch(request_fetch_remove_request(subject))
        return F.fetch(getUrlBySubject(subject).DELETE, {body: query})
            .then(json => {
                // debugger


                return dispatch(request_fetch_removed_receive(subject, json, query));
            })
            .catch(e => {
                console.log(e);
                alert(e);
            })
    }
}
export function fetch_remove(subject, query = {}) {
    return (dispatch, getState) => {
        dispatch(fetch_remove_action(subject, query))
    }
}


export function enable_delete_btn(subject, text) {
    return {type: ENABLE_DELETE_BTN, ...text, subject}
}


/**
 * flag : true | false
 * @param flag
 */
export function selectAllItem(subject, flag) {
    return {
        type: SELECT_ALL_ITEM,
        subject,
        flag
    }
}

/***
 * combine action
 */
export function selectItem(subject, index) {
    return dispatch=> {
        dispatch(select_(subject, index))
    }
}
export function select_(subject, index) {
    return {
        type: SELECT_ITEM,
        ...index,
        subject
    }
}

export function openModal(subject) {
    return {
        type: OPEN_MODAL,
        subject
    }
}
export function closeModal(subject) {
    return {
        type: CLOSE_MODAL,
        subject
    }
}
export function clientInfoSet(info) {
    return {
        type:CLIENT_INFO_SET,
        info
    }
}